﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Clinica_Frba.Registro_de_LLegada
{
    public partial class Registrar : Form
    {
        public Registrar()
        {
            InitializeComponent();
        }
        string afiliado;
        string profesional;
        string turno;
        public Registrar(string idafil,string idprof,string idturno)
        {
            InitializeComponent();
            afiliado = idafil;
            profesional = idprof;
            turno = idturno;
        }
        
        private void Registrar_Load(object sender, EventArgs e)
        {
            string comando;
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            comando = "SELECT * FROM DUNDER_MIFFLIN.BONO_CONSULTA WHERE USADO='N' AND GRUPO_FAMILIAR=LEFT('" + afiliado + "',LEN('" + afiliado + "')-2)";

            SqlDataAdapter da = new SqlDataAdapter(comando, myConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
         }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            int max,ult;
            if (dataGridView1.SelectedRows.Count > 1) { return; }
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                try
                {
                    string fechaat = publico.fecha;
                    SqlCommand b = new SqlCommand("BEGIN TRAN confirm", myConnection);
                    b.ExecuteNonQuery();
                    SqlCommand u = new SqlCommand("UPDATE DUNDER_MIFFLIN.TURNO SET ESTADO='C' WHERE ID_TURNO='" + turno + "'", myConnection);
                    u.ExecuteNonQuery();
                    SqlCommand getmax = new SqlCommand("SELECT ISNULL(MAX(NRO_CONSULTA),0) as maximo FROM DUNDER_MIFFLIN.BONO_CONSULTA WHERE GRUPO_FAMILIAR=LEFT('" + afiliado + "',LEN('" + afiliado + "')-2)", myConnection);
                    max = Convert.ToInt32(getmax.ExecuteScalar());
                    max++;
                    string comando = "UPDATE DUNDER_MIFFLIN.BONO_CONSULTA SET USADO='S',NRO_CONSULTA=" + max + " WHERE ID_BONO=" + Convert.ToInt32(row.Cells[0].Value).ToString();
 
                    SqlCommand u2 = new SqlCommand(comando, myConnection);
                    

                    u2.ExecuteNonQuery();
                    SqlCommand pid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='ATENCION'", myConnection);
                    ult = Convert.ToInt32(pid.ExecuteScalar());
                    ult++;
                    SqlCommand iis = new SqlCommand("SET IDENTITY_INSERT DUNDER_MIFFLIN.ATENCION ON", myConnection);
                    iis.ExecuteNonQuery();
                    SqlCommand ins = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.ATENCION(ID_ATENCION,ID_TURNO,FECHA,HORA,ESTADO,ID_PROF,ID_AFIL) VALUES(" + ult + "," + turno + ",CONVERT(datetime,'" + fechaat + "',103),CONVERT(time,getdate(),103),'D','" + profesional + "','" + afiliado + "')", myConnection);
                    ins.ExecuteNonQuery();
                    SqlCommand up = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + ult + " WHERE TABLA='ATENCION'", myConnection);
                    up.ExecuteNonQuery();
                    SqlCommand cmit = new SqlCommand("COMMIT TRAN confirm",myConnection);
                    cmit.ExecuteNonQuery();
                    MessageBox.Show("atencion ok");
                }
                catch (Exception ex){
                    MessageBox.Show(ex.Message,"Registrar atencion");
                    SqlCommand rb = new SqlCommand("ROLLBACK TRAN confirm",myConnection);
                    rb.ExecuteNonQuery();
                }
                this.Close();
                
            }
        }
    }
}
